import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile
import matplotlib.mlab as mlab
import scipy.stats as st
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def fivenum(v):
    """Returns Tukey's five number summary (minimum, lower-hinge, median, upper-hinge, maximum) for the input vector, a list or array of numbers based on 1.5 times the interquartile distance"""
    import numpy as np
    from scipy.stats import scoreatpercentile
    try:
        np.sum(v)
    except TypeError:
        print('Error: you must provide a list or array of only numbers')
    q1 = scoreatpercentile(v,25)
    q3 = scoreatpercentile(v,75)
    md = np.median(v)
    return np.min(v), q1, md, q3, np.max(v),

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    #inputCSV = "C:/Documents and Settings/wang322/My Documents/My Dropbox/STAT515/proj/st_louis_mo_temp_prep.csv"
    #inputCSV = "D:/My Documents/My Dropbox/STAT515/proj/st_louis_mo_temp_prep.csv"
    inputCSV = "C:/temp.csv"
    dataMatrix = build_data_list(inputCSV) # [year, temp, prep]
    #iLen = dataMatrix.shape
    
    #print scoreatpercentile(dataMatrix[:,2],75)
    #print fivenum(dataMatrix[:,1])
    #print fivenum(dataMatrix[:,2])
    #print np.std(dataMatrix[:,1])
    #value = dataMatrix[:,1]
    #mu = np.average(value)
    #sigma = np.std(value)
    #count, bins, ignored = plt.hist(value, 20, normed=True)
    #plt.show()
    #plt.boxplot(dataMatrix[:,2], notch=0, sym='b+', vert=1, whis=1.5,
                            #positions=None, widths=0.7, patch_artist=False, bootstrap=None, hold=None)
    #plt.xlabel('precipitation (inch)')
    #plt.title('Histogram of Temperature')
    #plt.title('Boxplot of Precipitation Data')
    #plt.title('Histogram of Precipitation')
    #plt.xlabel('Precipitation (inch)')
    #plt.ylabel('Temperature (F)')
    #plt.ylabel('frequency')
    #plt.axis([40, 160, 0, 0.03])
    #y = mlab.normpdf(bins, mu, sigma)
    #l = plt.plot(bins, y, 'r--', linewidth=1)
##    value = []
##    alpha = 0.05
##    for item in dataMatrix[:,1]:
##        value.append(item)
##    left, right = st.rv_continuous.interval()
##    print left, right
##    value = dataMatrix[:,1:3]
##    #print value
##    plt.plot(*zip(*value), marker='o', color='r', ls='')
##    plt.xlabel('Temperature (F)')
##    plt.ylabel('Precipitation (inch)')
    '''
    # Q-Q plot
    values = dataMatrix[:,2]
    fig = plt.figure()                 # set up plot
    ax = fig.add_subplot(1, 1, 1)
    (osm, osr), (m, b, r) = st.probplot(values, dist='norm')  # compute
    osmf = osm.take([0, -1])  # endpoints
    osrf = m * osmf + b       # fit line
    plt.title('Normal Q-Q Plot of Precipitation')
    plt.ylabel('Precipitation Quantiles')
    plt.xlabel('Normal Theoretical Quantiles')
    ax.plot(osm, osr, '.', osmf, osrf, '-')
    '''
    #a=0.8; b=-4
    
    (a_s,b_s,r,tt,stderr) = st.linregress(dataMatrix[:,4],dataMatrix[:,5])
    print('Linear regression using stats.linregress')
    print('regression: a=%.2f b=%.2f, std error= %.3f \n r-value: %.2f p-value: %.12f' % (a_s,b_s,stderr,r,tt))
    #y = a_s*dataMatrix[:,1] + b_s
    #plt.plot(dataMatrix[:,1], y, 'g-')
    
    #filePath = inputCSV[:-4] + "_total.csv"
    #np.savetxt(filePath, p_value, delimiter=',')
    #plt.show()

    print "end at " + getCurTime()
    print "========================================================================"  

           
